<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('疫情防控人员管理添加页面')" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!-- 引入 Bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .container {
            padding-right: 0px;
            padding-left: 0px;
        }
    </style>
</head>
<body class="white-bg" style="font-size: 16px">
<!--<h4 style="text-align: center; background-color: #0C72D1;color: white; padding:10px;"><span th:text="${deptName}"></span></span></h4>-->
<input id="curDeptId" type="hidden" th:value="${deptId}"></input>
<div class="container" id="permDiv" style="display: block;margin: 10px 5px 5px 5px;">
    <div class="form-group">
        <label for="userJobNum" class="col-sm-2 control-label"><em style="color: red;">*</em>工号</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="userJobNum" placeholder="请输入员工工号">
        </div>
    </div>
    <div id="userNameDeptDiv" style="display: none">
        <div class="form-group">
            <label for="userName" class="col-sm-2 control-label">姓名</label>
            <div class="col-sm-10">
                <input type="text" disabled class="form-control" id="userName" >
            </div>
        </div>
        <div class="form-group">
            <label for="userDept" class="col-sm-2 control-label">部门</label>
            <div class="col-sm-10">
                <input type="text" disabled class="form-control" id="userDept" >
            </div>
        </div>
    </div>
    <div style="text-align: center; margin: 10px;" id="searchBtn">
        <button style="width: 100%;font-size: 16px" type="button" class="btn btn-primary" onclick="searchUser()">查找</button>
    </div>
    <div style="text-align: center; margin: 10px; display: none" id="addUserBtn">
        <button style="width: 100%;font-size: 16px" type="button" class="btn btn-primary" onclick="submitAddUser()">添加</button>
    </div>
    <div style="text-align: center; margin: 10px; display: none" id="resetBtn">
        <button style="width: 100%;font-size: 16px" type="button" class="btn btn-primary" onclick="resetBtn()">重置</button>
    </div>
</div>
<!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- 包括所有已编译的插件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "wxcp/epidemic/collect";
    //添加用户界面
    function searchUser() {
        var userJobNumVal = $('#userJobNum').val();
        var curDeptId = $('#curDeptId').val();
        if (userJobNumVal == undefined || userJobNumVal == null || userJobNumVal == '') {
            $.modal.alertWarning("请先输入员工工号");
            return;
        } else {
            //根据该员工工号，从后台查出该工号对应的用户名、所在部门
            $.post(prefix+"/user/getUserInfoByJobNum",{userJobNumVal:userJobNumVal, curDeptId:curDeptId},function(result){
                if (result.data != null) {
                    var type = result.data.type;
                    if (type == -1) {
                        $.modal.alertWarning("系统查无此人，请输入正确的工号");
                        return;
                    } else if (type == 1) {
                        $.modal.alertWarning("当前用户已存在您管理的部门下");
                        $('#userName').val(result.data.auditUserDTO.userName);
                        $('#userDept').val(result.data.auditUserDTO.deptName);
                        $("#userNameDeptDiv").attr("style","display:block;");
                        $("#addUserBtn").attr("style","display:none;");
                        $("#searchBtn").attr("style","display:none;");
                        $("#userJobNum").removeAttr("disabled");
                        $("#resetBtn").attr("style","display:block;");
                    } else if (type == 2) {
                        $.modal.alertWarning("当前用户不在您管理的部门下,是否将其切换到您当前管理的部门下");
                        $('#userName').val(result.data.auditUserDTO.userName);
                        $('#userDept').val(result.data.auditUserDTO.deptName);
                        $("#userNameDeptDiv").attr("style","display:block;");
                        $("#resetBtn").attr("style","display:none;");
                        $("#searchBtn").attr("style","display:none;");
                        $("#userJobNum").attr("disabled","disabled");
                        $("#addUserBtn").attr("style","display:block;");
                    } else if (type == 3) {
                        $('#userName').val(result.data.auditUserDTO.userName);
                        $('#userDept').val(result.data.auditUserDTO.deptName);
                        $("#userNameDeptDiv").attr("style","display:block;");
                        $("#userJobNum").removeAttr("disabled");
                        $("#resetBtn").attr("style","display:none;");
                        $("#searchBtn").attr("style","display:none;");
                        $("#addUserBtn").attr("style","display:block;");
                    } else {
                        resetBtn();
                    }
                }
            });
        }
    }

    function resetBtn() {
        $('#userName').val("");
        $('#userDept').val("");
        $("#userNameDeptDiv").attr("style","display:none;");
        $("#userJobNum").removeAttr("disabled");
        $("#addUserBtn").attr("style","display:none;");
        $("#resetBtn").attr("style","display:none;");
        $("#searchBtn").attr("style","display:block;");
    }

    function submitAddUser() {
        //新增用户
        var userJobNumVal = $('#userJobNum').val();//对应用户userId
        var curDeptId = $('#curDeptId').val();
        var srcDeptName = $('#userDept').val();
        $.modal.confirm("确定添加该成员到当前部门下吗?", function() {
            $.post(prefix+"/user/addUserInfoByJobNum",{userJobNumVal:userJobNumVal, curDeptId:curDeptId, srcDeptName: srcDeptName},function(result){
                if (result.code == 0) {
                    window.location.href = prefix + "/userManage";
                }
            });
        });
    }
</script>
</body>
</html>
